package com.tianruan.dc.modules.sys.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianruan.dc.common.utils.IdGen;
import com.tianruan.dc.modules.sys.user.entity.WorkInfo;
import com.tianruan.dc.modules.sys.user.mapper.WorkInfoMapper;
import com.tianruan.dc.modules.sys.user.service.WorkInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Author: Vivienne
 */
@Service
@Slf4j
public class WorkInfoServiceImpl extends ServiceImpl<WorkInfoMapper, WorkInfo> implements WorkInfoService {
    @Autowired
    private WorkInfoMapper workInfoMapper;

    @Override
    public void insertContent(String username, String content, String userId) {
        if(content!=null||userId!=null){
            WorkInfo workInfo = new WorkInfo();
            workInfo.setId(IdGen.uuid());
            workInfo.setUsername(username);
            workInfo.setWorkId(userId);
            workInfo.setContent(content);
            save(workInfo);
        }
    }

    @Override
    public int updateWorkInfo(WorkInfo workInfo) {
        UpdateWrapper<WorkInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username", workInfo.getUsername());
        updateWrapper.eq("work_id", workInfo.getWorkId());
        updateWrapper.set("content",workInfo.getContent());
        int row=workInfoMapper.update(null,updateWrapper);
        return row;
    }

    @Override
    public WorkInfo getWorkInfo(String username, String workId) {
        WorkInfo info=workInfoMapper.selectInfo(username, workId);

//        WorkInfo info = query().eq("work_id", workInfo.getWorkId()).eq("username", workInfo.getUsername()).one();
        return info;

    }

    @Override
    public void updateScore(String id, String score, String content) {
        UpdateWrapper<WorkInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("score",score);
        updateWrapper.set("comments",content);
        workInfoMapper.update(null,updateWrapper);
    }

    /**
     * 获得tile
     * @param id
     * @return
     */
    @Override
    public String getTitle(String id) {
        WorkInfo workInfo = workInfoMapper.getUsernameAndWorkid(id);
        return workInfoMapper.getTitle(workInfo.getUsername(),workInfo.getWorkId());
    }

}
